package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import domains.Message;

import mappers.MessageMapper;

public class ComposeMsgGUI {
	private JFrame composeMsgFrame;
	private JTextArea textArea;
	private JLabel subjectLbl, textAreaLbl;
	private JTextField subjectTF;
	private JScrollPane scrollPane;
	private JButton composeBtn, cancelBtn;

	public ComposeMsgGUI() {
		composeMsgFrame = new JFrame();
		composeMsgFrame.setTitle("Compose Message");
		composeMsgFrame.setLayout(new BorderLayout());
		composeMsgFrame.setSize(500, 500);	
		
		JPanel p = new JPanel(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		GridBagConstraints c1 = new GridBagConstraints();
		
		subjectLbl = new JLabel("Subject: ");
		subjectTF = new JTextField(20);
		
		c.weightx = 0.5;
		c.gridx = 0;
		c.gridy = 0;
		p.add(subjectLbl, c);
		
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 0;
		p.add(subjectTF, c);
		
		textAreaLbl = new JLabel("Your message here: ");
		textArea = new JTextArea(10, 20);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		scrollPane = new JScrollPane(textArea);
		
		c1.weightx = 0.5;
		c1.gridx = 0;
		c1.gridy = 1;
		c1.insets = new Insets(-140, 0, 0, 0);
		p.add(textAreaLbl, c1);
		
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 1;
		p.add(scrollPane, c);
		
		//c.weighty = 1.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		JPanel p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		composeBtn = new JButton("Compose");	
		composeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				Calendar cal = Calendar.getInstance();
				Message m = new Message(subjectTF.getText(), textArea.getText(), dateFormat.format(cal.getTime()));
				MessageMapper msgMapper = new MessageMapper();
				msgMapper.composeMessage(m);
				composeMsgFrame.setVisible(false);
			}
		});
		
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				composeMsgFrame.setVisible(false);
			}
		});

		p1.add(composeBtn, c);
		p1.add(cancelBtn, c);
		composeMsgFrame.add(p, BorderLayout.CENTER);
		composeMsgFrame.add(p1, BorderLayout.SOUTH);
		composeMsgFrame.pack();
		composeMsgFrame.setVisible(true);
	}
}
